package com.cqs.leetcode.queueStack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * lixw
 * 2021/11/6
 **/
public class DecodeString394 {


    public String decodeString(String s) {
        //Input: s = "2[abc]3[cd]ef"
        //Output: "abcabccdcdcdef"
        //"2[20[bc]31[xy]]xd4[rt]"
        Stack<Character> stack = new Stack<>();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= '0' && c <= '9' || c == '[') {
                stack.push(c);
            } else if (c == ']') {
                StringBuilder simpleChar = new StringBuilder();
                int repeat = 0;
                StringBuilder repeatBuilder = new StringBuilder();
                while (!stack.isEmpty()) {
                    char peek = stack.pop();
                    if (peek >= '0' && peek <= '9') {
                        int cnt = -1;
                        repeat +=(int)(Math.pow(10, ++cnt) * (peek - '0'));
                        while (!stack.isEmpty() && stack.peek() >= '0' && stack.peek() <= '9') {
                            peek = stack.pop();
                            repeat +=  Math.pow(10, ++cnt) * (peek - '0');
                        }
                        //处理完数字了
                        break; // 2[abc] 或者 100[leetcode]
                    } else if (peek == '[') {
                        //啥也不干
                    } else {
                        simpleChar.append(peek);
                    }
                }
                String simpleStr = simpleChar.reverse().toString();
                for (int j = 0; j < repeat; j++) {
                    repeatBuilder.append(simpleStr);
                }
                if (!stack.isEmpty()) {
                    for (int j = 0; j < repeatBuilder.length(); j++) {
                        stack.push(repeatBuilder.charAt(j));
                    }
                } else {
                    sb.append(repeatBuilder);
                }
            } else {
                if (stack.isEmpty()) {
                    sb.append(c);
                } else {
                    stack.push(c);
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        List<String> cases = new ArrayList<>();
//        cases.add("21[xy]");
//        cases.add("2[10[bc]21[xy]]");
        cases.add("2[20[bc]31[xy]]xd4[rt]");
        cases.add("10[leetcode]");
        cases.add("3[a]2[bc]");
        cases.add("3[a2[c]]");
        cases.add("3[av2[cd]]");
        cases.add("2[abc]3[cd]ef");
        cases.add("abc3[cd]xyz");
        DecodeString394 d = new DecodeString394();
        for (String s : cases) {
            System.out.println(s + " ----> " + d.decodeString(s) +  " ===> " +d.decodeString(s).length() );
        }


        System.out.println("xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy".length());
    }
}
